package com.yj.jason.jsondemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;
import com.scwang.smartrefresh.header.DropboxHeader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.footer.FalsifyFooter;
import com.yj.jason.baselibrary.view.MyRecyclerView.SwipeMenuRecyclerView;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.CommonRecyclerAdapter;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.ViewHolder;
import com.yj.jason.baselibrary.view.RecyclerView.WrapRecyclerView;
import com.yj.jason.baselibrary.view.banner.BannerAdapter;
import com.yj.jason.baselibrary.view.banner.BannerView;
import com.yj.jason.jsondemo.adapter.OnItemCallbackHelper;
import com.yj.jason.jsondemo.adapter.SlidingAdapter;
import com.yj.jason.jsondemo.fragment.MySmartRefrehsLayout;

import java.util.ArrayList;
import java.util.List;

public class SmartRefreshActivity extends AppCompatActivity implements SlidingAdapter.IonSlidingViewClickListener{

    private WrapRecyclerView recyclerView;
    private List<Person> mBeans=new ArrayList<>();
    private List<String> mList=new ArrayList<>();

    private LinearLayoutManager mLayoutManager;

    private SlidingAdapter slidingAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_refresh);





        recyclerView=findViewById(R.id.recyclerView);



        final SmartRefreshLayout refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
//设置 Header 为 贝塞尔雷达 样式
      //  refreshLayout.setRefreshHeader(new MyHeaderView(this));

       refreshLayout.setRefreshHeader(new MaterialHeader(this));
//设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshFooter(new ClassicsFooter(this) {
        });






        for(int i=1;i<20;i++){
            Person person=new Person("总页防水等级"+i,i);

            if(i<5){
                person.setTittle("我是头部11");
            }else if(4<i&&i<10){
                person.setTittle("我是头部22");
            }else if(9<i&&i<15){
                person.setTittle("我是头部33");
            }else if(14<i&&i<20){
                person.setTittle("我是头部44");
            }
            mBeans.add(person);
        }



        GroupListener groupListener = new GroupListener() {
            @Override
            public String getGroupName(int position) {
                if(mBeans.size()>position&&position>-1){
                    //获取分组名
                    return mBeans.get(position).getTittle();
                }else {
                    return null;
                }

            }
        };
        StickyDecoration decoration = StickyDecoration.Builder
                .init(groupListener)
                //重置span（使用GridLayoutManager时必须调用）
                //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
                .build();


        mLayoutManager=new LinearLayoutManager(this);

        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.addItemDecoration(decoration);


        recyclerView.setAdapter(new CommonRecyclerAdapter<Person>(this,mBeans,R.layout.item_2) {
            @Override
            public void convert(ViewHolder holder, final Person item) {
                holder.setText(R.id.tv_item,item.getName());
                holder.setOnIntemClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(SmartRefreshActivity.this,"点击了"+item.getName(),Toast.LENGTH_SHORT).show();
                    }
                });
            }


        });


       /* slidingAdapter = new SlidingAdapter(this);

        recyclerView.setAdapter(slidingAdapter);


        ItemTouchHelper.Callback callback = new OnItemCallbackHelper(slidingAdapter);

        *//**
         * 实例化ItemTouchHelper对象,然后添加到RecyclerView
         *//*
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(recyclerView);*/


        View headView=LayoutInflater.from(this).inflate(R.layout.header_view,recyclerView,false);

        BannerView banner_vp = (BannerView)headView.findViewById(R.id.banner_vp);

        recyclerView.addHeaderView(headView);


        //2、通过Resources获取
        DisplayMetrics dm = getResources().getDisplayMetrics();

        int width=dm.widthPixels;

        banner_vp.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,width));


        banner_vp.setAdapter(new BannerAdapter() {
            @Override
            public View getView(int position) {
                ImageView iv=new ImageView(SmartRefreshActivity.this);
                iv.setImageResource(R.drawable.ic_launcher);
                return iv;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public String getBannerDesc(int mCurrentPostion) {
                return "2222";
            }
        });


        banner_vp.startRoll();




    }


    @Override
    public void onItemClick(View view, int position) {

    }

    @Override
    public void onDeleteBtnCilck(View view, int position) {
        slidingAdapter.removeData(position);
    }
}
